package prober;

import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import queries.Document;
import queries.DocumentSample;
import util.HtmlParser;
import util.Logger;
import util.NestedRuntimeException;

/**
 * The content summary builder.
 * 
 * @author Vika
 * @author ak2765
 */
public class ContentSummaryBuilder {

	/**
	 * Builds content summary for the category passed as parameter.
	 * 
	 * @param category The category to check
	 * @return The content summary for the category passed as parameter
	 */
	public ContentSummary build(Category category) {
		//Logger.info("Building content summary for category: " + category);

		ContentSummary contentSummary = new ContentSummary(category);

		DocumentSample sample = category.getSample();

		Set<Document> documents = sample.getDocuments();

		for (Document document : documents) {

			String[] words = loadTokens(document.getUrl());

			for (String word : words) {
				contentSummary.addWord(word);
			}
		}

		return contentSummary;
	}

	private String[] loadTokens(String url) {
		//Logger.info("Retrieving tokens for url: " + url);

		List<String> filteredTokens = new LinkedList<String>();
		try {
			String[] tokens = new HtmlParser().parse(new URL(url));

			for (String token : tokens) {
				token = formatToken(token);
				if ("".equals(token)) {
					continue;
				}
				filteredTokens.add(token);
			}

		} catch (Exception e) {
			Logger.error("Couldn't load page from url: " + url, e);

			throw new NestedRuntimeException("Couldn't load page from url: "
					+ url, e);
		}
		return filteredTokens.toArray(new String[filteredTokens.size()]);
	}

	private String formatToken(String token) {
		char[] chars = token.toCharArray();
		StringBuilder newToken = new StringBuilder();
		for(int i=0;i<chars.length;i++) {
			if (Character.isLetter(chars[i])) {
				newToken.append(chars[i]);
			}
		}
		return newToken.toString();
	}
}
